package com.riwon.im.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.riwon.im.common.constant.UserConstant;
import com.riwon.im.common.result.ResultData;
import com.riwon.im.pojo.Msg;
import com.riwon.im.pojo.User;
import com.riwon.im.service.IMsgService;
import com.riwon.im.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpy
 * @since 2018-05-09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userServiceImpl;
    @Resource
    private IMsgService msgServiceImpl;

    /**
     * 用户注册（web端和锐家后台都会使用）
     * @param nickname
     * @param userType
     * @param shopid
     * @param relateId
     * @return
     */
    @PostMapping("/register")
    public ResultData register(@RequestParam String nickname,
                               @RequestParam Integer userType,
                               @RequestParam Integer shopid,
                               @RequestParam String appids,
                               @RequestParam(required = false) Integer relateId){
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("type",userType);
        wrapper.ne("state",UserConstant.State.DELETE.getCode());
        wrapper.eq("shop_id",shopid);
        if(Objects.nonNull(relateId)){
            wrapper.eq("relate_id",relateId);
        }
        //非客服 appids只有一个值
        if(UserConstant.Type.USER.getCode() != userType){
            wrapper.eq("appids",appids);
        }
        User user = userServiceImpl.selectOne(wrapper);
        if(Objects.nonNull(user)){
            return ResultData.build().put("userId",user.getId());
        }
        user = new User();
        user.setShopId(shopid);
        user.setRelateId(relateId);
        user.setType(userType);
        user.setName(nickname);
        user.setAppids(appids);
        user.setState(UserConstant.State.OFFLINE.getCode());
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userServiceImpl.insert(user);
        return ResultData.build().put("userId",user.getId());
    }


    //===================================================================锐家APP获取用户id

    /**
     * 获取
     * @param shopid
     * @param relateId
     * @return
     */
    @GetMapping("/staff")
    public ResultData getUserId(@RequestParam Integer shopid,
                                @RequestParam Integer relateId){
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("relate_id",relateId);
        wrapper.eq("shop_id",shopid);
        wrapper.ne("state",UserConstant.State.DELETE.getCode());
        wrapper.eq("type",UserConstant.Type.USER.getCode());
        User user = userServiceImpl.selectOne(wrapper);
        return ResultData.build().put("user",user);
    }

    //===================================================================营销管理后台接口

    /**
     * 获取所有客服
     * @param shopid
     * @return
     */
    @GetMapping("/staffs")
    public ResultData staff(@RequestParam Integer shopid){
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("type",UserConstant.Type.USER.getCode());
        wrapper.ne("state",UserConstant.State.DELETE.getCode());
        wrapper.eq("shop_id",shopid);
        List<User> users = userServiceImpl.selectList(wrapper);
        return ResultData.build().parseList(users);
    }

    /**
     * 获取单个客服的统计数据
     * @return
     */
    @GetMapping("/staff/data")
    public ResultData staffData(@RequestParam Date startOn,
                                @RequestParam Date endOn,
                                @RequestParam Integer shopid,
                                @RequestParam Integer relateId){

        Wrapper<User> userWrapper = new EntityWrapper<>();
        userWrapper.eq("shop_id",shopid)
                .eq("relate_id",relateId);
        User user = userServiceImpl.selectOne(userWrapper);
        if(Objects.isNull(user) || user.getState().equals(UserConstant.State.DELETE.getCode())){
            return ResultData.build()
                    .put("fansNumber",0)
                    .put("msgNumber",0);
        }
        //服务粉丝数量
        Wrapper<Msg> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("COUNT(id) fansNumber");
        wrapper.eq("shop_id",shopid);
        wrapper.eq("to_id",relateId);
        wrapper.ge("create_time",startOn);
        wrapper.le("create_time",endOn);
        wrapper.groupBy("user_id");
        Integer fansNumber = (Integer) msgServiceImpl.selectMap(wrapper).get("fansNumber");
        //回复消息条数
        wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id",shopid);
        wrapper.eq("from_id",relateId);
        wrapper.ge("create_time",startOn);
        wrapper.le("create_time",endOn);
        int msgNumber = msgServiceImpl.selectCount(wrapper);
        return ResultData.build()
                .put("fansNumber",fansNumber)
                .put("msgNumber",msgNumber);
    }

    /**
     * 删除客服
     * @param shopid
     * @param relateId
     * @return
     */
    @DeleteMapping("/staff")
    public ResultData deleteUser(@RequestParam Integer shopid,
                                 @RequestParam Integer relateId){
        Wrapper<User> userWrapper = new EntityWrapper<>();
        userWrapper.eq("shop_id",shopid)
                .eq("relate_id",relateId);
        User user = userServiceImpl.selectOne(userWrapper);
        if(Objects.nonNull(user)){
            user.setState(UserConstant.State.DELETE.getCode());
            user.setUpdateTime(new Date());
            userServiceImpl.updateById(user);
        }
        return ResultData.build().success();
    }
}

